package com.mdc.MultiDataSource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

/**
 * @author Mordecai
 * @version 1.0.0
 * @create 2017/10/27 16:47
 * @blog
 */
@Configuration
public class DataSourceConfig {
    /*多数据源配置*/
    @Bean(name = "primaryDataSource")
    /*@Qualifier("primaryDataSource")*/
    @Primary
    @ConfigurationProperties(prefix="spring.datasource.11")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "secondaryDataSource")
    /*@Qualifier("secondaryDataSource")*/
    /*@Primary*/
    @ConfigurationProperties(prefix="spring.datasource.22")
    public DataSource secondaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "thirdDataSource")
    @ConfigurationProperties(prefix="spring.datasource.33")
    public DataSource thirdDataSource() {
        return DataSourceBuilder.create().build();
    }

    /*添加JdbcTemplate支持*/
    @Primary
    @Bean(name = "primaryJdbcTemplate")
    public JdbcTemplate primaryJdbcTemplate(
            @Qualifier("primaryDataSource") DataSource ds) {
        return new JdbcTemplate(ds);
    }

    @Bean(name = "secondaryJdbcTemplate")
    public JdbcTemplate secondaryJdbcTemplate(
            @Qualifier("secondaryDataSource") DataSource ds) {
        return new JdbcTemplate(ds);
    }

    @Bean(name = "thirdJdbcTemplate")
    public JdbcTemplate thirdJdbcTemplate(
            @Qualifier("thirdDataSource") DataSource ds) {
        return new JdbcTemplate(ds);
    }

    /*@Primary
    @Bean(name = "transactionManagerPrimary")
    public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
    }*/

    @Bean(name = "primaryTransactionManager")
    public PlatformTransactionManager primaryTransactionManager
            (@Qualifier("primaryDataSource") DataSource ds) {
        return new DataSourceTransactionManager(ds);
    }

    @Bean(name = "secondaryTransactionManager")
    public PlatformTransactionManager secondaryTransactionManager
            (@Qualifier("secondaryDataSource") DataSource ds) {
        return new DataSourceTransactionManager(ds);
    }

    @Bean(name = "thirdTransactionManager")
    public PlatformTransactionManager thirdTransactionManager
            (@Qualifier("thirdDataSource") DataSource ds) {
        return new DataSourceTransactionManager(ds);
    }
}
